package com.gjx.clock.ui

import android.app.ActivityManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.*
import android.widget.*
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.gjx.clock.ClockViewModel
import com.gjx.clock.R
import com.gjx.clock.service.ForwardService
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.now.*
import layout.FragmentAdapter


import android.net.Uri
import android.os.Build
import android.provider.Settings
import com.gjx.clock.BuildConfig
import android.view.WindowManager
import androidx.preference.PreferenceManager


@Suppress("DEPRECATION")
class MainActivity : AppCompatActivity() {
    private val viewModel by lazy { ViewModelProvider(this).get(ClockViewModel::class.java) }

    override fun onResume() {
        // 打开后台自启权限

//        val intent = Intent()
//        intent.action = android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS
//        intent.data = Uri.parse("package:" + this.packageName)
//        startActivity(intent)

        if(PreferenceManager.getDefaultSharedPreferences(this).getBoolean("hidePermissionHint",false)){
            test.visibility=View.GONE
        }else{
            test.visibility=View.VISIBLE
        }
        test.text="设置了后台运行权限才能使用"
        test.setOnClickListener {
            try {
                val intent = Intent()
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                intent.putExtra("packageName", BuildConfig.APPLICATION_ID)
                val comp = ComponentName(
                    "com.huawei.systemmanager",
                    "com.huawei.permissionmanager.ui.MainActivity"
                )//华为权限管理
                intent.component = comp
                startActivity(intent)
            } catch (e: Exception) {
                val intent = Intent(Settings.ACTION_SETTINGS)
                startActivity(intent)
                e.printStackTrace()

                // 应用详细设置
//                startActivity(getAppDetailSettingIntent())
            }
        }
        super.onResume()
    }
    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        val decorView = window.decorView
        // 设置UI占全屏
        decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        // 设置状态栏透明色
        window.statusBarColor = Color.TRANSPARENT

        setContentView(R.layout.activity_main)

        val fragments = ArrayList<Fragment>()
        fragments.apply {
            add(NormalAlarmClockFragment())
            add(RestAlarmClockFragment())
        }
        val titles = ArrayList<String>()
        titles.apply {
            add("普通闹钟")
            add("间隔闹钟")
        }
        val pagerAdapter = FragmentAdapter(supportFragmentManager,titles,fragments)
        viewPager.adapter = pagerAdapter

        tabLayout.setupWithViewPager(viewPager,false)

        // 全局设置
        settingsBtn.setOnClickListener {
            val intent = Intent(this, SettingsActivity::class.java)
            startActivityForResult(intent, 1)
        }

        // 启动前台服务
        var isServiceRunning = false
        val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val services = activityManager.getRunningServices(10)
        for (item in services) {
            if(item.service.className.equals(ForwardService::class.java.canonicalName)){
                isServiceRunning = true
            }
        }
        if(!isServiceRunning){
            val intent = Intent(this, ForwardService::class.java)
            startService(intent)
        }



    }
    private fun getAppDetailSettingIntent(): Intent? {
        val localIntent =  Intent()
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS")
            localIntent.setData(Uri.fromParts("package",packageName, null))
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW)
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails")
            localIntent.putExtra("com.android.settings.ApplicationPkgName", packageName)
        }
        return localIntent
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(requestCode){
            // 全局设置
            1 -> {
                Toast.makeText(this,"设置成功", Toast.LENGTH_SHORT).show()
            }
        }
    }
}
